﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace Teein.Proxy.Bot.Helper
{
    public class IEProxy
    {
        public struct Struct_INTERNET_PROXY_INFO
        {
            public int dwAccessType;
            public IntPtr proxy;
            public IntPtr proxyBypass;
        };
        [DllImport("wininet.dll", SetLastError = true)]
        private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
        public void RefreshIESettings(string strProxy)
        {
            const int INTERNET_OPTION_PROXY = 38;
            const int INTERNET_OPEN_TYPE_PROXY = 3;
            Struct_INTERNET_PROXY_INFO struct_IPI;
            // Filling in structure
            struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
            struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);
            struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");
            // Allocating memory
            IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));
            // Converting structure to IntPtr
            Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
            bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI));
        }

        public bool CleanCookies(string URLStr, int ExType)
        {
            //定义变量
            string CookiesPath, FindDirctroy, OsTypeStr;
            string UserProfile;
            string XPCookiesPath, VistaCookiesPath;
            long OsType;

            //获取用户配置路径
            UserProfile = Environment.GetEnvironmentVariable("USERPROFILE");
            //获取操作系统类型
            OsType = Environment.OSVersion.Version.Major;

            //解析地址
            if (URLStr == "")
                FindDirctroy = "";
            else
            {
                //用"."分割字符
                char[] separator = { '.' };
                string[] MyWords;
                MyWords = URLStr.Split(separator);
                //选取其中的关键字
                try
                {
                    FindDirctroy = MyWords[1];
                }
                catch
                {
                    FindDirctroy = "";
                }
            }

            //判断浏览器类型
            if (ExType == 0)
            {
                //IE浏览器
                XPCookiesPath = @"\Cookies\";
                VistaCookiesPath = @"\AppData\Roaming\Microsoft\Windows\Cookies\";
            }
            else if (ExType == 1)
            {
                //FireFox浏览器
                XPCookiesPath = @"\Application Data\Mozilla\Firefox\Profiles\";
                VistaCookiesPath = @"\AppData\Roaming\Mozilla\Firefox\Profiles\";
                FindDirctroy = "cookies";
            }
            else
            {
                XPCookiesPath = "";
                VistaCookiesPath = "";
                return false;
            }

            //判断操作系统类型
            if (OsType == 5)
            {
                //系统为XP
                OsTypeStr = "Microsoft Windows XP";
                CookiesPath = UserProfile + XPCookiesPath;
            }
            else if (OsType == 6)
            {
                //系统为Vista
                OsTypeStr = "Microsoft Windows Vista";
                CookiesPath = UserProfile + VistaCookiesPath;
            }
            else if (OsType == 7)
            {
                //系统为Win 7
                OsTypeStr = "Microsoft Windows 7";
                CookiesPath = UserProfile + VistaCookiesPath;
            }
            else
            {
                //未识别之操作系统
                OsTypeStr = "Other OS Version";
                CookiesPath = "";
                return false;
            }

            //删除文件
            if (DeleteFileFunction(CookiesPath, FindDirctroy))
                return true;
            else
                return false;
        }
        //重载函数
        public bool CleanCookies()
        {
            if (CleanCookies("", 0))
                return true;
            else
                return false;
        }
        private bool DeleteFileFunction(string filepath, string FindWords)
        {
            string Dstr;
            //下面这些字串例外
            string ExceptStr = "index.dat";
            //解析删除关键字
            if (FindWords == "")
                Dstr = "*.*";
            else
                Dstr = "*" + FindWords + "*";

            //删除cookie文件夹
            try
            {
                foreach (string dFileName in Directory.GetFiles(filepath, Dstr))
                {
                    if (dFileName == filepath + ExceptStr)
                        continue;
                    File.Delete(dFileName);
                }
            }
            catch (Exception e)
            {
                return false;
            }

            //深层遍历（解决Vista Low权限问题）
            string[] LowPath = Directory.GetDirectories(filepath);
            foreach (string ThePath in LowPath)
            {
                try
                {
                    foreach (string dFileName in Directory.GetFiles(ThePath, Dstr))
                    {
                        if (dFileName == filepath + ExceptStr)
                            continue;
                        File.Delete(dFileName);
                    }
                }
                catch (Exception e)
                {
                    return false;
                }
            }
            //测试使用
            return true;
        }
    }
}
